﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;

namespace WebTV_Kickstarter
{
    public partial class Project
    {
        private static EFDataModelContainer tempContainer = null;

        public static void AddProject(Project project)
        {
            EFDataModelContainer db = new EFDataModelContainer();
            db.ProjectSet.Add(project);
            db.SaveChanges();
        }

        internal static void AddProjectWidthDbContainer(Project project, EFDataModelContainer eFDataModelContainer)
        {
            EFDataModelContainer db = eFDataModelContainer;

             db.ProjectSet.Add(project);
             db.SaveChanges();
            
        }

        internal static Project GetProject(int? id)
        {
            EFDataModelContainer db = new EFDataModelContainer();
            return db.ProjectSet.Find(id);
        }

        internal static void EditProject(Project p)
        {
            EFDataModelContainer db = new EFDataModelContainer();
            db.Entry(p).State = EntityState.Modified;
            db.SaveChanges();
        }

        public  static void EditProjectFromDonation(Project p)
        {
            EFDataModelContainer db = null;

            if (tempContainer != null)
            {
                db = tempContainer;
                db.Entry(p).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public static void DisposeTempDbContainer()
        {
            tempContainer = null;
        }


        public static IQueryable<Project> Index()
        {
            EFDataModelContainer db = new EFDataModelContainer();
            IQueryable<Project> projectset = db.ProjectSet.Include(p => p.User);
            return projectset;
        }

        public static Project FindProject(int? id)
        {
            EFDataModelContainer db = new EFDataModelContainer();
            tempContainer = db;
            return db.ProjectSet.Find(id);
        }

        public static DbSet GetGebruikerSet()
        {
            EFDataModelContainer db = new EFDataModelContainer();
            return db.GebruikerSet;
        }

        public static void DeleteProject(Project p)
        {
            EFDataModelContainer db = null;

            if (tempContainer != null)
            {
                db = tempContainer;
                tempContainer = null;
                db.ProjectSet.Remove(p);
                db.SaveChanges();
            }
        }

        public static void Dispose()
        {
            EFDataModelContainer db = new EFDataModelContainer();
            db.Dispose();
        }


        public static EFDataModelContainer GetTempDbContainer()
        {
            if (tempContainer != null)
            {
                return tempContainer;
            }
            else
            {   
                tempContainer = new EFDataModelContainer();
                return tempContainer;
            }
        }

        public static List<Project> GetProjects()
        {
            EFDataModelContainer db = new EFDataModelContainer();
            return db.ProjectSet.ToList();
        }

        internal static void CreateProject(Project project, Gebruiker tempGebruiker)
        {
            project.TotalAmount = 0;
            project.TotalRating = 0;
            project.NumberOfVotes = 0;
            project.NumberOfMessages = 0;

            project.User = Gebruiker.FindGebruikerWithDbContainer(tempGebruiker.Id, Project.GetTempDbContainer());
            Project.AddProjectWidthDbContainer(project, Project.GetTempDbContainer());
        }

        internal static WebTV_Kickstarter.Goal CreateGoalForProject(Project project, string goalTitle, string goalAmount, string goalDescription)
        {
            Goal goal = new Goal();
            goal.Title = goalTitle;
            goal.Description = goalDescription;
            goal.Amount = Convert.ToInt32(goalAmount);
            goal.isAchieved = false;
            goal.Project = project;

            return goal;
        }

        internal static void AddScore(int id, int score, EFDataModelContainer db)
        {
            Project project = db.ProjectSet.Find(id);
            project.TotalRating += score;
            project.NumberOfVotes++;
            db.SaveChanges();
        }

        internal static void EditScore(int id, int scoreVerschil, EFDataModelContainer db)
        {
            Project project = db.ProjectSet.Find(id);
            project.TotalRating += scoreVerschil;
        }
    }
}